/**
 * 
 */
package mx.frantor.catalogos.daoimp;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import mx.frantor.catalogos.dao.ClientesDAO;
import mx.frantor.catalogos.entitys.ClientesEntity;
import mx.frantor.util.HibernateUtil;

/**
 * @author Hp
 *
 */
public class ClientesDAOImpl implements ClientesDAO {

	/**
	 * 
	 */

	@Override
	public String agregarClientes(ClientesEntity nuevoClientes) {
		// TODO Auto-generated method stub
		String e = "";
		Session session = HibernateUtil.getSessionFactory()
				.getCurrentSession();
		
		session.beginTransaction();
		try {
		session.save(nuevoClientes);
		session.flush();
		session.getTransaction().commit();
		e = nuevoClientes.getIdCliente().toString();
		} catch (Exception x) {
			//e.printStackTrace();
			session.getTransaction().rollback();
			e = x.getMessage();
			//return e.toString();
		}
		HibernateUtil.getSessionFactory().getCurrentSession().close();

		System.out.println("agregar clientes resultado " + e);

		return e;
	}

	@Override
	public String modificarClientes(ClientesEntity modificarClientes) {
		// TODO Auto-generated method stub
		String e = "";
		Session session = HibernateUtil.getSessionFactory()
				.getCurrentSession();
	
		session.beginTransaction();
		try {
			ClientesEntity clientesTemp = (ClientesEntity) session.get(
					ClientesEntity.class, modificarClientes.getIdCliente());
		session.flush();
		session.evict(clientesTemp);
		session.update(modificarClientes);
		session.getTransaction().commit();
		} catch (Exception x) {
			//e.printStackTrace();
			session.getTransaction().rollback();
			e = x.getMessage();
			//return e.toString();
		}
		HibernateUtil.getSessionFactory().getCurrentSession().close();

		System.out.println("modificar clientes resultado " + e);

		return e;
	}

	@Override
	public String eliminarClientes(long idCliente) {
		// TODO Auto-generated method stub
		String e = "";
		Session session = HibernateUtil.getSessionFactory()
				.getCurrentSession();
		

			// TODO Auto-generated method stub			
			session.beginTransaction();
			try {
				ClientesEntity ClientesElimina = (ClientesEntity) session.get(
						ClientesEntity.class, idCliente);
			session.delete(ClientesElimina);
			session.getTransaction().commit();
		} catch (Exception x) {
			//e.printStackTrace();
			session.getTransaction().rollback();
			e = x.getMessage().toString();
			//return e.toString();
		}
		//HibernateUtil.getSessionFactory().getCurrentSession().close();
		HibernateUtil.getSessionFactory().getCurrentSession().close();
		System.out.println("eliminar clientes resultado " + e);

		
		return e;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ClientesEntity> getTodosClientesActivos() {
		// TODO Auto-generated method stub
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<ClientesEntity> clientes = session.createCriteria(ClientesEntity.class)
				.addOrder(Order.asc("IdCliente")).list();
		HibernateUtil.getSessionFactory().getCurrentSession().close();
		return clientes;
	}

	@SuppressWarnings("unchecked")
	@Override
	public ClientesEntity getClientesId(long idClientes) {
		// TODO Auto-generated method stub
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<ClientesEntity> clientes = (List<ClientesEntity>) session.createCriteria(ClientesEntity.class)
				.add(Restrictions.eq("IdCliente", idClientes))
				.setFetchSize(1).list();
		HibernateUtil.getSessionFactory().getCurrentSession().close();
		if(clientes==null){
			return null;
		}else{
			return clientes.get(0);
		}
	}

	@Override
	public List<ClientesEntity> getClientesPorId(long idClientes) {
		// TODO Auto-generated method stub
		return null;
	}

}
